package com.op.studyingliteapp.activity;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.op.studyingliteapp.R;
import com.op.studyingliteapp.common.Common;
import com.op.studyingliteapp.common.SeminarsUrl;
import com.op.util.asynctask.HttpPostTask;
import com.op.util.asynctask.OnTaskProcessListener;

public class SendPostActivity extends Activity{
	private String getCourseId;
	private EditText title,context;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_send_post);
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true); 
		actionBar.setDisplayShowHomeEnabled(false); 
		actionBar.setDisplayShowTitleEnabled(true); 
		actionBar.setTitle("发送帖子");
		SharedPreferences preferences =getSharedPreferences("StudyingLite", Context.MODE_PRIVATE);
		getCourseId=preferences.getString(Common.Prefs.COURSE_ID, "");
		title= (EditText) findViewById(R.id.title);
		context = (EditText) findViewById(R.id.context);
		sendPost();
		
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			onBackPressed();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	public void sendPost(){
		
		
		Button send= (Button) findViewById(R.id.send);
		send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(title.length()>0&&context.length()>0 ){
					sureSend();
				}else{
					AlertDialog.Builder builder = new Builder(SendPostActivity.this);
					builder.setMessage("标题和内容不能为空");
					builder.setTitle("提示");
					builder.setNegativeButton("确定", null);
					builder.create();
					builder.show();
				}
			}
		});
	}
	
	
	public void sureSend(){
		
		AlertDialog.Builder builder = new Builder(SendPostActivity.this);
		builder.setMessage("确认发送帖子吗？");
		builder.setTitle("提示");
		builder.setNeutralButton("确认", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				String  Title = title.getText().toString();
				String   Content= context.getText().toString();
				sendToOnlineData(Title,Content,getCourseId);
				finish();
			}
		});
		builder.setNegativeButton("取消", null);
		builder.create();
		builder.show();
	}
	
	
	/**
	 * 发送字符串到网上数据库 Title,Content,userId,courseId，parentId
	 * 依次是标题，内容，课程id
	 */
	public void sendToOnlineData(String title, String Content, String courseId) {
		SharedPreferences preferences = getSharedPreferences("StudyingLite", Context.MODE_PRIVATE);
		String getUserId = preferences.getString(Common.Prefs.USER_ID, "");//用户id
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair(Common.Seminar.ADD_UPDATE_SEMINAR_CONTENT, Content));
		params.add(new BasicNameValuePair(Common.Seminar.ADD_UPDATE_SEMINAR_USER_ID, getUserId));
		params.add(new BasicNameValuePair(Common.Seminar.ADD_UPDATE_SEMINAR_TITLE, title));
		params.add(new BasicNameValuePair(Common.Seminar.ADD_UPDATE_SEMINAR_COURSE_ID, courseId));
		params.add(new BasicNameValuePair(Common.Seminar.ADD_UPDATE_SEMINAR_PARENT_ID, null));

		HttpPostTask task = new HttpPostTask(SendPostActivity.this, SeminarsUrl.ADD_SEMINAR, params);
		task.setTaskListener(new OnTaskProcessListener() {

			@Override
			public void prepare() {
				// TODO Auto-generated method stub

			}

			@Override
			public void completed(String result) {
				//ToastUtils.show(getActivity(), "发布帖子成功");
//				Intent intent = new Intent(SendPostActivity.this, SendPostActivity.class);
//				startActivity(intent);
			}
		});
		task.execute();
	}
	
	
}
